package com.kamloo.stg.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.view.*;
import android.widget.*;
import com.kamloo.stg.data.db.Line;
import com.kamloo.stg.R;

public class TabBusLineArrayAdapter extends ArrayAdapter<String> implements Filterable
{
	//----- GLOBAL VARIABLES -----
	private String[] values;
	private ArrayList<Line> m_lines;
    
    //----- CLASS CODE -----
	public TabBusLineArrayAdapter(Context context, String[] values, ArrayList<Line> lines) 
	{
		super(context, R.layout.tab_bus_row, values);		
		 
		this.values = values.clone();
		this.m_lines = lines;		
	}
		
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
	   
	    if (m_lines != null)
	    {
		    View rowView = inflater.inflate(R.layout.tab_bus_row, parent, false);
		    // View
		    TextView identifierView = (TextView) rowView.findViewById(R.id.tabBusLineRowIdentifier);
		    TextView nameView = (TextView) rowView.findViewById(R.id.tabBusLineRowName);
		    TextView ligneIdView = (TextView) rowView.findViewById(R.id.tabBusLineRowLineId);
		    
		    // Set visible data
		    ligneIdView.setText(Integer.toString(m_lines.get(position).getId()));
		    
		    String identifier = m_lines.get(position).getIdentifier();		    
		    if (identifier.contains("STS"))
		    {
		    	identifier = identifier.replace("STS", "");
		    	identifier = identifier.substring(0, identifier.indexOf("_"));
		    }
		    identifierView.setText(identifier);
		    nameView.setText(values[position]);
		    
		    return rowView;
	    }
	    
	    return null;
	}	
}
